home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 3.9 KB | 151 lines |
- package symantec.itools.awt;
-
-
- import java.util.Enumeration;
- import java.util.Vector;
-
-
- /**
- * Creates a text box, containing a list of item, with up and down arrows.
- * Use this component to allow your users to move through a set of fixed
- * values or type a valid value in the box.
- * <p>
- * At run time, only the selected value is displayed in the text box.
- * <p>
- * @see symantec.itools.awt.Spinner
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
-
- public class ListSpinner
- extends Spinner
- {
- //--------------------------------------------------
- // constants
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // class variables
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // member variables
- //--------------------------------------------------
-
- /**
- * The list of strings that get displayed in the spinner.
- */
- protected Vector list;
-
-
- //--------------------------------------------------
- // constructors
- //--------------------------------------------------
-
- /**
- * Constructs an empty ListSpinner.
- */
- public ListSpinner()
- {
- list = new Vector();
- }
-
-
- //--------------------------------------------------
- // accessor methods
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // event methods
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // class methods
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // member methods
- //--------------------------------------------------
-
- /**
- * Tells this component that it has been added to a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is added to a container. Typically, it is used to
- * create this component's peer.
- * Here it's used to get the length of the largest string in the list.
- *
- * @see java.awt.Container#removeNotify
- */
- public void addNotify()
- {
- if (list.size() > 0) {
-
- for (Enumeration e = list.elements(); e.hasMoreElements();)
- {
- textWidth = Math.max(textWidth, ((String)e.nextElement()).length());
- }
-
- text = (String)list.elementAt(current);
- setMax(list.size() - 1);
- }
-
- super.addNotify();
- }
-
- /**
- * Add a String to the list.
- * @param s the String to be appended to the list
- * @see #setListItems
- */
- public void addItem(String s)
- {
- list.addElement(s);
- textWidth = Math.max(textWidth, s.length());
- setMax(list.size() - 1);
- }
-
- /**
- * Gets the currently selected String from the list.
- * @return the String currently visible in the Spinner
- */
- public String getCurrentText()
- {
- return list.size() > 0 ? (String)list.elementAt(current) : null;
- }
-
- /**
- * Adds the given string array to the list.
- * @param items items to add to the list
- * @see #getListItems
- */
- public void setListItems(String[] items)
- {
- for (int i = 0; i < items.length; ++i)
- {
- addItem(items[i]);
- }
- updateText();
- }
-
- /**
- * Returns the current list as a array of Strings.
- * @return the current list
- * @see #setListItems
- */
- public String[] getListItems()
- {
- int len = list.size();
- String[] items = new String[len];
- for (int i = 0; i < len; ++i)
- {
- items[i] = (String)list.elementAt(i);
- }
- return items;
- }
- }